package com.ruoyi.paipan.common;

import com.ruoyi.paipan.utils.SolarUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tiemeng
 * * @date 2024/11/21
 */
public class SolarMonth {
    private int year;
    private int month;

    public SolarMonth(int year, int month) {
        this.year = year;
        this.month = month;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public String toString()
    {
        return this.year + "-" + this.month;
    }


    public String toFullString()
    {
        return this.year + "年" + this.month + "月";
    }

    public static SolarMonth fromYm(int year, int month)
    {
        return new SolarMonth(year, month);
    }

    public static SolarMonth fromDate(Date date)
    {
        Solar solar = Solar.fromDate(date);
        return new SolarMonth(solar.getYear(), solar.getMonth());
    }

    public List<Solar> getDays()
    {
        List<Solar> l = new ArrayList<>();
        Solar d = Solar.fromYmd(this.year, this.month, 1);
        l.add(d);
        int days = SolarUtil.getDaysOfMonth(this.year, this.month);
        for (int i = 1; i < days; i++) {
            l.add( d.next(i));
        }
        return l;
    }

    public List<SolarWeek> getWeeks(int start)
    {
        List<SolarWeek> l = new ArrayList<>();
        SolarWeek week = SolarWeek.fromYmd(this.year, this.month, 1, start);
        while (true) {
            l.add(week);
            week = week.next(1, false);
            Solar firstDay = week.getFirstDay();
            if (firstDay.getYear() > this.year || firstDay.getMonth() > this.month) {
                break;
            }
        }
        return l;
    }

    public SolarMonth next(int months)
    {
        int n = months < 0 ? -1 : 1;
        int m = Math.abs(months);
        int y = this.year + (int)(m / 12) * n;
        m = this.month + m % 12 * n;
        if (m > 12) {
            m -= 12;
            y++;
        } else if (m < 1) {
            m += 12;
            y--;
        }
        return new SolarMonth(y, m);
    }

    public static void main(String[] args) {
        System.out.println(SolarMonth.fromYm(2024, 11));
    }
}
